package com.able.concurrency.t1;

import java.io.IOException;

/**
 * @author abel.he
 * @data 2018/8/15 18:31
 * @describe 静态synchronized方法和synchronized(Object.class) 方法等同
 */
public class Test2 {

    private static int count = 0;

    private static synchronized void test1() {
        System.out.println(Thread.currentThread().getName() + " count=" + count);
        try {
            System.in.read();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void test2() {
        synchronized (Test2.class) {
            System.out.println(Thread.currentThread().getName() + "count:" + count++);
        }
    }

    public static void main(String[] args) {
        final Test2 test2 = new Test2();
        for (int i = 0; i < 10; i++) {
            new Thread(new Runnable() {
                public void run() {
                    test2.test1();
                }
            }).start();
        }
        for (int i = 0; i < 10; i++) {
            new Thread(new Runnable() {
                public void run() {
                    test2.test2();
                }
            }).start();
        }
    }

}
